package org.scboy.dbms.controller;

import java.io.IOException;
import java.util.List;

import org.scboy.common.annotation.Menu;
import org.scboy.common.annotation.Permission;
import org.scboy.common.request.Result;
import org.scboy.dbms.service.BaseAppService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;

@RestController
@RequestMapping("/base/app")
@Menu("微服务管理系统.微服务管理")
public class BaseAppController {
	
	Log log=LogFactory.get();
	
	@Autowired
	BaseAppService service;
	
	@PostMapping("page")
	@Permission("分页查询")
	public Result page(@RequestBody Model params) {
		Page page=service.page(params);
		return Result.Success(page);
	}
	
	@PostMapping("add")
	@Permission("新增")
	public Result add(@RequestBody Model params) {
		boolean result=service.add(params);
		if(result) {
			return Result.Success("SERVER_APP_ADD_SUCCESS");
		}else{
			return Result.Error("SERVER_APP_ADD_ERROR");
		}
	}
	
	@PutMapping("update")
	@Permission("修改")
	public Result update(@RequestBody Model params) {
		boolean result=service.update(params);
		if(result) {
			return Result.Success("SERVER_APP_UPDATE_SUCCESS");
		}else {
			return Result.Error("SERVER_APP_UPDATE_ERROR");
		}
	}
	
	@DeleteMapping("delete")
	@Permission("删除")
	public Result delete(String appName) {
		boolean result=service.delete(appName);
		if(result) {
			return Result.Success("SERVER_APP_DELETE_SUCCESS");
		}else {
			return Result.Error("SERVER_APP_DELETE_ERROR");
		}
	}
	
	@GetMapping("get")
	@Permission("获取详细信息")
	public Result get(String appName) {
		Model data=service.get(appName);
		return Result.Success(data, 0L);
	}
	
	@GetMapping("getAppPaths")
	@Permission("获取权限配置路径")
	public Result getAppPaths(String type) {
		List<Model> datas=service.getAppPaths(type);
		return Result.Success(datas);
	}
	

	@GetMapping("list")
	@Permission("所有微服务列表")
	public Result list() {
		List<Model> datas=service.list();
		return Result.Success(datas);
	}
	
	@PostMapping("upload")
	@Permission("上传微服务Jar包")
	public Result upload(@RequestParam("file") MultipartFile jarFile,@RequestParam("appName") String appName) {
		boolean result;
		try {
			result = service.upload(appName,jarFile.getBytes());
			if(result) {
				return Result.Success("SERVER_APP_UPLOAD_SUCCESS");
			}else{
				return Result.Error("SERVER_APP_UPLOAD_ERROR");
			}
		} catch (IOException e) {
			log.error(e);
			return Result.Error("SERVER_APP_UPLOAD_ERROR");
		}
		
	}
}
